package com.spa.common.generate.config;

import java.util.Date;

import com.mysql.cj.util.StringUtils;
import com.spa.common.generate.po.TableInfo;
import com.spa.common.generate.utils.CommonUtils;

import lombok.Data;

/**
 * 生成代码相关信息
 * 
 * @author zhaowei@xhzq.com
 * @date 2023/03/28
 */
@Data
public class EosGlobalConfig {
    // 项目
    private String item = "";
    // 文件编码
    private String systemEncoding = "utf-8";
    // 模板
    private String templatepath = "/template";
    // 文件输出路径，不配置的话默认输出当前项目的resources/code目录下
    private String outputDir;
    // 实体
    private String entity = "entity";
    // 实体包
    private String entityPackage = "entity";
    // dao层包名
    private String mapperPackage = "dao";
    // dao层xml路径
    private String mapperXmlPath = "";
    // serviceImpl层包名
    private String serviceImplPackage = "service.Impl";
    // rabbitmq层包名
    private String rabbitmqPackage = "rabbitmq";
    // vo层包名
    private String voPackage = "vo";
    // vo层包名
    private String viewPackage = "";
    // repository层包名
    private String repositoryPackage = "repository";
    // service层包名
    private String servicePackage = "service";
    // 控制层包名
    private String controllerPackage = "controller";
    // 项目路径
    private String packagePath = "zqsoft.eos.v01.";
    // 表名
    private String tableName;
    // 表信息
    private TableInfo tableInfo;
    // 作者
    private String author = "";
    // 项目介绍
    private String introduce = "";
    // 项目名称
    private String projectName = "EOS";
    // 创建时间
    private String date = CommonUtils.getFormatTime("yyyy-MM-dd HH:mm:ss", new Date());

    public String getMapperPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.mapperPackage : this.mapperPackage + "." + this.item;
    }
    
    public String getEntityPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.entityPackage : this.entityPackage + "." + this.item;
    }
    
    public String getRabbitmqPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.rabbitmqPackage : this.rabbitmqPackage + "." + this.item;
    }
    
    public String getVoPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.voPackage : this.voPackage + "." + this.item;
    }
    
    public String getRepositoryPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.repositoryPackage : this.repositoryPackage + "." + this.item;
    }
    
    public String getServicePackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.servicePackage : this.servicePackage + "." + this.item;
    }
    
    public String getControllerPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.controllerPackage : this.controllerPackage + "." + this.item;
    }
    
    public String getViewPackage() {
        return (StringUtils.isNullOrEmpty(this.item)) ? this.viewPackage : this.viewPackage + "." + this.item;
    }
    
    public String getTableName() {
        return this.entity.toLowerCase();
    }

    /**
     * 获取模板名称
     * 
     * @param type
     * @return
     */
    public String getTemplateName(String type) {
        return EosCodeServiceEnum.valueOf(type).getTemplateName();
    }

    /**
     * 获取生成的文件路径
     * 
     * @param type
     * @return
     */
    public String getCodePath(String type) {
        return EosCodeServiceEnum.valueOf(type).getCodePath(this);
    }

    public String getDirPath(String type) {
        return EosCodeServiceEnum.valueOf(type).getDirPath(this);
    }

    public String getPackagePaths() {
        return this.packagePath.replaceAll("\\.", "/");
    }

    /**
     * 替代if 枚举方案
     * 
     * @author zhaowei@xhzq.com
     * @date 2023/04/06
     */
    public enum EosCodeServiceEnum implements EosCodeService {
        controller() {
            @Override
            public String getTemplateName() {
                return "controllerTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getControllerPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("Controller").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getControllerPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },

        service() {
            @Override
            public String getTemplateName() {
                return "serviceTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getServicePackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("Service").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getServicePackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },
        
        dao() {
            @Override
            public String getTemplateName() {
                return "daoTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getMapperPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("Dao").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getMapperPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },

        repository() {
            @Override
            public String getTemplateName() {
                return "repositoryTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getRepositoryPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("Repository").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getRepositoryPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },

        vo() {
            @Override
            public String getTemplateName() {
                return "voTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getVoPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("VO").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getVoPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },

        rabbitmq() {
            @Override
            public String getTemplateName() {
                return "rabiitmqTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getRabbitmqPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("Receiver").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getRabbitmqPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },
        entity() {
            @Override
            public String getTemplateName() {
                return "entityTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getEntityPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append(eosGlobalConfig.getEntity()).append("").append(".java");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/java/").append(eosGlobalConfig.getPackagePaths())
                    .append(eosGlobalConfig.getEntityPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        },
        view() {
            @Override
            public String getTemplateName() {
                return "viewTemplate.ftl";
            }

            @Override
            public String getCodePath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/view/").append(eosGlobalConfig.getViewPackage().replaceAll("\\.", "/")).append("/");
                // 文件名
                path.append("index.vue");

                return path.toString();
            }

            @Override
            public String getDirPath(EosGlobalConfig eosGlobalConfig) {
                StringBuilder path = new StringBuilder();
                // 项目根目录
                path.append(eosGlobalConfig.getOutputDir());
                // 包名 package.path
                path.append("/view/").append(eosGlobalConfig.getViewPackage().replaceAll("\\.", "/")).append("/");

                return path.toString();
            }
        }
    }
}
